<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>活动管理</el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-row>
        <el-col>
          <el-button type="primary" @click="cateHandle">添加分类</el-button>
        </el-col>
      </el-row>
      <tree-table
        :data="cateList"
        show-index
        border
        :show-row-hover="false"
        index-text="#"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
      >
        <template slot="isok" slot-scope="scoped">
          <i class="el-icon-success" style="color:lightgreen" v-if="scoped.row.cat_deleted===false"></i>
          <i class="el-icon-error" v-else></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scoped">
          <el-tag size="mini" v-if="scoped.row.cat_level===0">一级</el-tag>
          <el-tag size="mini" type="warning" v-else-if="scoped.row.cat_level===1">二级</el-tag>
          <el-tag size="mini" type="success" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt">
          <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
        </template>
      </tree-table>
    </el-card>
    <!-- 分页地方 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="cateInfo.pagenum"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="cateInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 添加用户对话框 -->
    <el-dialog
      title="修改"
      :visible.sync="aditCateDialogVisible"
      width="50%"
      @closed="handleAditDialogClose"
    >
      <span>
        <el-form
          :model="aditCateForm"
          :rules="aditCateFormRules"
          ref="addCateFormRef"
          label-width="120px"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="aditCateForm.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <el-cascader
              v-model="selectCateKeys"
              :options="addCateListFrom"
              :props="{ expandTrigger: 'hover',label:'cat_name',checkStrictly:true,value:'cat_id' }"
              @change="handleCateChange"
              clearable
            ></el-cascader>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="aditCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="cateAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      cateInfo: {
        type: 3,
        pagenum: 2,
        pagesize: 5
      },
      //   商品分类数据列表默认数据为空
      cateList: [],
      //   默认总数据条数
      total: 0,
      // 添加用户对话框
      aditCateDialogVisible: false,
      // 用户对话框
      aditCateForm: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0
      },
      // 添加用户的数据
      addCateListFrom: [],
      // 保存数据选中的id值
      selectCateKeys: [],
      // 验证规则
      aditCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '用户名在3~10个字符之间',
            trigger: 'blur'
          }
        ]
      },
      //   对应的列
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        // type:'template'(将该列设置为模板列)，template:'isok'(设置该列模板的名称为isok)
        { label: '是否有效', prop: '', type: 'template', template: 'isok' },
        { label: '排序', prop: '', type: 'template', template: 'order' },
        { label: '操作', prop: '', type: 'template', template: 'opt' }
      ]
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    async getCateList () {
      const { data: res } = await this.$http.get('categories', {
        params: this.cateInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品数据失败')
      }
      this.cateList = res.data.result
      this.$message({
        message: '商品数据获取成功',
        type: 'success'
      })
      this.total = res.data.total
      console.log(res)
    },
    handleSizeChange (page) {
      this.cateInfo.pagesize = page
      // 调用函数数据
      this.getCateList()
    },
    handleCurrentChange (num) {
      this.cateInfo.pagenum = num
      // 调用函数数据
      this.getCateList()
    },
    handleAditDialogClose () {
      this.$refs.addCateFormRef.resetFields()
      this.selectCateKeys = []
      this.addCateListFrom.cat_pid = 0
      this.addCateListFrom.cat_level = 0
    },
    // 添加用户
    cateHandle () {
      this.aditCateDialogVisible = true
      this.preasCateHandle()
    },
    // 获取数据
    async preasCateHandle () {
      const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
      this.addCateListFrom = res.data
      console.log(res)
    },
    // 确定添加
    cateAdd () {
      this.$refs.addCateFormRef.validate(async isValue => {
        if (!isValue) return
        const { data: res } = await this.$http.post('categories', this.aditCateForm)
        if (res.meta.status !== 201) {
          this.$message({
            message: res.meta.msg,
            type: 'error',
            showClose: true
          })
        } else {
          this.$message({
            message: '创建分类成功',
            type: 'success',
            showClose: true
          })
          this.getCateList()
        }
      })
      this.aditCateDialogVisible = false
    },
    // 监听事件
    handleCateChange () {
      if (this.selectCateKeys.length > 0) {
        this.aditCateForm.cat_pid = this.selectCateKeys[this.selectCateKeys.length - 1]
        this.aditCateForm.cat_level = this.selectCateKeys.length
      } else {
        this.aditCateForm.cat_pid = 0
        this.aditCateForm.cat_level = 0
      }
      console.log(this.selectCateKeys)
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
